use "$project_path/data/4_regdata/regdata", clear

** Appendix Figure C.3(a)

// Finding the time elapsed since last treatment
use "$project_path/data/3_cleaned/turnovers_parliamentary_electoral.dta", clear
keep Country Year Month Type_Election treatment
sort Country Year Month 
gen elapsed_last_treatment = .
forvalues k = 1/20 {
	replace elapsed_last_treatment = Year[_n] - Year[_n-`k'] if elapsed_last_treatment==. & treatment[_n-`k']==1 & Country[_n]==Country[_n-`k']
}
keep Country Year Month Type_Election elapsed_last_treatment
tempfile elapsed_last_treatment_parl
save `elapsed_last_treatment_parl'
use "$project_path/data/3_cleaned/turnovers_presidential.dta"
keep Country Year Month Type_Election treatment
sort Country Year Month
gen elapsed_last_treatment = .
forvalues k = 1/20 {
	replace elapsed_last_treatment = Year[_n] - Year[_n-`k'] if elapsed_last_treatment==. & treatment[_n-`k']==1 & Country[_n]==Country[_n-`k']
}
keep Country Year Month Type_Election elapsed_last_treatment
append using `elapsed_last_treatment_parl'
merge 1:1 Country Year Month Type_Election using "$project_path/data/4_regdata/regdata", keep(using match) nogen

// RD estimate
rdrobust elapsed_last_treatment runvar, all
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot elapsed_last_treatment runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) legend(off) /*title("Time since last treatment (years)", size(large) placement(9) span))*/)
graph export "$project_path/output/appendix_figures/Appendix_Figure_C3a.pdf", as(pdf) name("Graph") replace

** Appendix Figure C.3(b)

use "$project_path/data/4_regdata/regdata", clear

rdrobust last_runvar runvar, all
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot last_runvar runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) legend(off) /*title("Previous election running variable", size(large) placement(9) span))*/)
graph export "$project_path/output/appendix_figures/Appendix_Figure_C3b.pdf", as(pdf) name("Graph") replace

** Appendix Figure C.3(c)

use "$project_path/data/4_regdata/regdata", clear

rdrobust last_treatment runvar, all
local rdd_tau : di %8.3f e(tau_cl)
local rdd_lb : di %8.3f e(ci_l_rb)
local rdd_lb = subinstr("`rdd_lb'", " ", "", .)
local rdd_ub : di %8.3f e(ci_r_rb)
local rdd_ub = subinstr("`rdd_ub'", " ", "", .)
local rdd_pval : di %8.3f e(pv_rb)
if e(pv_rb)<0.001 local rdd_pval = "<0.001"
local N = e(N)
local rd_est = "RD est.: `rdd_tau' [`rdd_lb', `rdd_ub']. p-value: `rdd_pval'. N = `N'"

rdplot last_treatment runvar if inrange(runvar, -20, 20), p(1) nbins(10 10) graph_options(xtitle("Margin of victory of the challenger", size(large)) ylabel(, angle(0) labsize(large)) graphregion(color(white)) note("`rd_est'", tstyle(size(medlarge))) xlabel(-20(5)20, labsize(large)) legend(off) /*title("Previous election running variable", size(large) placement(9) span))*/)
graph export "$project_path/output/appendix_figures/Appendix_Figure_C3c.pdf", as(pdf) name("Graph") replace


